// pages/vote_candidate/vote_candidate.js
var app = getApp();
var host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schemesData: [

    ],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var projectId = options.projectId;
    var that = this;
    that.setData({
      projectId: projectId
    });
    wx.request({
      url: host + 'city360/project/queryProjectOwnsSchemes',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        projectId: projectId
      },
      complete: res=>{
        if(res.statusCode === 200){
          for(let i = 0; i < res.data.length; i++){
            res.data[i].src = host + res.data[i].imageFileName;
            res.data[i].selected = false;
            res.data[i].index = i + 1;
          }
          console.log("收到数组数据", res.data);
          that.setData({
            schemesData: res.data
          });
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tapScheme: function (event){
    console.log("点击scheme", event)
    var imageFileName = event.currentTarget.dataset.imagefilename
    var that = this;
    var schemeIndex = -1;
    for(var i = 0; i < that.data.schemesData.length; i++){
      if(that.data.schemesData[i].imageFileName === imageFileName){
        schemeIndex = i;
        break;
      }
    }
    that.data.schemesData[schemeIndex].selected = !that.data.schemesData[schemeIndex].selected;
    that.setData({
      schemesData: that.data.schemesData
    });
  },

  voteEnsure: function(event){
    var that = this;
    var hasChoseSomething = false;
    that.data.schemesData.forEach(each=>{
      if(each.selected){
        hasChoseSomething = true;
      }
    });
    if(!hasChoseSomething){
      return;
    }
    wx.showLoading({
      title: '投票中',
    });
    that.data.schemesData.forEach(each=>{
      if(each.selected){
        wx.request({
          url: host + 'city360/project/vote',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            projectId: that.data.projectId,
            imageFileName: each.imageFileName
          },
          complete: res=>{
            console.log("投票完毕", res);
          }
        })
      }
    });
    wx.hideLoading();
    that.data.schemesData.forEach(each=>{
      each.selected = false
    });
    that.setData({
      schemesData: that.data.schemesData
    });
    wx.showToast({
      title: '投票成功',
    })
  },

  lookResult: function(event){
    var that = this;
    wx.navigateTo({
      url: '../voteResult/voteResult?projectId=' + that.data.projectId,
    })
  }

})